package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import com.atguigu.search.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private SearchFeignClient searchFeignClient;

    @GetMapping({"/", "index.html"})
    public String getSkuDetail(Model model) {
        RetVal retVal = productFeignClient.getIndexCategory();
        model.addAttribute("list", retVal.getData());
        return "index/index";
    }

    //2.商品的搜索功能
    @GetMapping("search.html")
    public String searchProduct(SearchParam searchParam, Model model) {
        //通过远程RPC调用shop-search实现搜索 这边只负责拿到数据展示在页面
        RetVal<Map> retVal = searchFeignClient.searchProduct(searchParam);
        model.addAllAttributes(retVal.getData());
        //1.搜索路径上参数回显
        String urlParam = pageUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //2.页面回显品牌信息
        String brandNameParam = pageBrandNameParam(searchParam.getBrandName());
        model.addAttribute("brandNameParam", brandNameParam);
        //3.页面回显平台属性信息
        List<Map<String,String>> propList = pagePlatformParam(searchParam.getProps());
        model.addAttribute("propsParamList", propList);
        //4.页面回显排序信息
        Map<String, Object> orderMap = pageSortParam(searchParam.getOrder());
        model.addAttribute("orderMap", orderMap);

        return "search/index";
    }

    //&order=2:asc
    private Map<String, Object> pageSortParam(String order) {
        Map<String, Object> orderMap = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] orderSplit = order.split(":");
            if (orderSplit.length == 2) {
                orderMap.put("type", orderSplit[0]);
                orderMap.put("sort", orderSplit[1]);
            } else {
                //默认给一个排序方式
                orderMap.put("type", 1);
                orderMap.put("sort", "desc");
            }
        }
        return orderMap;
    }

    //&props=4:骁龙888:CPU型号 &props=5:5.0英寸以下:屏幕尺寸
    private List<Map<String, String>> pagePlatformParam(String[] props) {
        List<Map<String, String>> propList = new ArrayList<>();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                //&props=4:骁龙888:CPU型号
                String[] propSplit = prop.split(":");
                if (propSplit.length == 3) {
                    Map<String, String> propMap = new HashMap<>();
                    propMap.put("propertyKeyId", propSplit[0]);
                    propMap.put("propertyValue", propSplit[1]);
                    propMap.put("propertyKey", propSplit[2]);
                    propList.add(propMap);
                }
            }
        }
        return propList;
    }

    private String pageBrandNameParam(String brandName) {
        if (!StringUtils.isEmpty(brandName)) {
            //&brandName=3:三星
            String[] brandSplit = brandName.split(":");
            if (brandSplit.length == 2) {
                return "品牌:" + brandSplit[1];
            }
        }
        return null;
    }

    //?keyword=三星 &brandName=3:三星 &props=4:骁龙888:CPU型号 &props=5:5.0英寸以下:屏幕尺寸 &order=2:asc
    private String pageUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        //判断是否有关键字
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }
        //判断是否有分类id
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //判断是否有品牌并拼接参数 &brandName=3:三星
        if (!StringUtils.isEmpty(searchParam.getBrandName())) {
            if (urlParam.length() > 0) {
                //原地址栏有其他参数
                urlParam.append("&brandName=").append(searchParam.getBrandName());
            }
        }
        //判断平台属性并拼接参数 &props=4:骁龙888:CPU型号 &props=5:5.0英寸以下:屏幕尺寸
        if (!StringUtils.isEmpty(searchParam.getProps())) {
            if (urlParam.length() > 0) {
                //原地址栏有其他参数
                for (String prop : searchParam.getProps()) {
                    urlParam.append("&props=").append(prop);
                }
            }
        }
        return "search.html?" + urlParam.toString();
    }
}
